//
//  FWHomeService.swift
//  DBS
//
//  Created by 王方伟 on 2017/4/19.
//  Copyright © 2017年 wfw. All rights reserved.
//

import UIKit

class FWHomeService {
    
    class func loadTitleData() -> [FWHomeTypeModel] {
        let path = Bundle.main.path(forResource: "types.plist", ofType: nil)!
        let dataArray = NSArray(contentsOfFile: path) as! [[String : Any]]
        var tempArray = [FWHomeTypeModel]()
        
        for dict in dataArray {
            tempArray.append(FWHomeTypeModel(dict: dict))
        }
        return tempArray
    }
    
    class func loadHomeData(type : FWHomeTypeModel, index : Int, success : @escaping (_ success : [FWAnchorModel])->(), failture : @escaping (_ error : Error)->()){
        var anchorModels = [FWAnchorModel]()
        FWHttpUtil.sharedInstance.getRequest(urlString: "http://qf.56.com/home/v4/moreAnchor.ios", params: ["type" : type.type, "index" : index, "size" : 48], success: { (response) in
            guard let messageDict = response["message"] as? [String : Any] else { return }
            guard let dataArray = messageDict["anchors"] as? [[String : Any]] else { return }
            
            for (index, dict) in dataArray.enumerated() {
                let anchor = FWAnchorModel(dict: dict)
                anchor.isEvenIndex = index % 2 == 0
                anchorModels.append(anchor)
            }
            success(anchorModels)
        }) { (error) in
            failture(error)
        }
    }
        
}
